package o;

import android.content.Context;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperManager;
import com.huawei.hms.framework.network.restclient.dnkeeper.RequestHost;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsResult;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsResult.Address;
import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpRetryException;
import java.net.MalformedURLException;
import java.net.NoRouteToHostException;
import java.net.PortUnreachableException;
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLKeyException;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLProtocolException;

public class dmv {
    private static LinkedHashMap<String, Integer> ˎ;
    private static final Pattern ˏ = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");
    private static boolean ॱ = false;

    static class d implements Callable<List<String>> {
        private String ˊ;
        private int ˎ;
        private Exception ˏ;

        public /* synthetic */ Object call() throws Exception {
            return ˏ();
        }

        public d(String str, Exception exception, int i) {
            this.ˊ = str;
            this.ˏ = exception;
            this.ˎ = i;
        }

        public List<String> ˏ() {
            return dmv.ˊ(this.ˊ, this.ˏ, this.ˎ);
        }
    }

    static {
        ˎ = null;
        ˎ = new LinkedHashMap();
        ˎ.put(SocketException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(1));
        ˎ.put(PortUnreachableException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(2));
        ˎ.put(SocketTimeoutException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(3));
        ˎ.put(HttpRetryException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(3));
        ˎ.put(ConnectException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(3));
        ˎ.put(UnknownHostException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(5));
        ˎ.put(NoRouteToHostException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(6));
        ˎ.put(UnknownServiceException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(7));
        ˎ.put(ProtocolException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(9));
        ˎ.put(SSLException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(10));
        ˎ.put(SSLKeyException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(11));
        ˎ.put(SSLPeerUnverifiedException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(12));
        ˎ.put(SSLProtocolException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(13));
        ˎ.put(SSLHandshakeException.class.getSimpleName().toUpperCase(Locale.US), Integer.valueOf(14));
    }

    public static void ˋ() {
        if (!ॱ) {
            DNKeeperManager.getInstance().init(ContentManager.getInstant().getContext(), 3000);
            ॱ = true;
        }
    }

    public static String ˎ(String str, String str2) {
        try {
            URI uri = new URI(str);
            URI uri2 = new URI(uri.getScheme(), null, str2, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
            Logger.ˎ("DNSUtil", "url host updated");
            str = uri2.toString();
        } catch (URISyntaxException e) {
        }
        return str;
    }

    public static int ˏ(int i, Exception exception) {
        if (exception != null) {
            String toUpperCase = exception.toString().toUpperCase(Locale.US);
            String toUpperCase2 = exception.getClass().getSimpleName().toUpperCase(Locale.US);
            if (ˎ.containsKey(toUpperCase2)) {
                return ((Integer) ˎ.get(toUpperCase2)).intValue();
            }
            for (Entry entry : ˎ.entrySet()) {
                if (toUpperCase.indexOf((String) entry.getKey()) >= 0) {
                    return ((Integer) entry.getValue()).intValue();
                }
            }
            if (exception instanceof IOException) {
                return 14;
            }
            if (i == ResponseBean.NOT_FOUND) {
                return 14;
            }
        }
        return 0;
    }

    public static List<String> ˊ(String str, Exception exception, int i) {
        List<String> arrayList = new ArrayList();
        if (dof.ˋ(str)) {
            return arrayList;
        }
        Context ˋ = ContentManager.getInstant().getContext();
        RequestHost requestHost = new RequestHost(str);
        requestHost.setApkName(ˋ.getPackageName());
        requestHost.setTime(3000);
        if (exception != null) {
            String valueOf = String.valueOf(i);
            requestHost.setDnsFailType(valueOf);
            Logger.ˋ("DNSUtil", "getIpListByDNSBackup, DNSRequest, failReason = " + valueOf);
        }
        long currentTimeMillis = System.currentTimeMillis();
        ˋ();
        DnsResult dnsResult = null;
        try {
            dnsResult = DNKeeperManager.getInstance().queryIpsSync(requestHost);
        } catch (Throwable th) {
            Logger.e("DNSUtil", "getIpListByDNSBackup, syncQueryDNS exception:" + th.getMessage());
        }
        Logger.ˎ("DNSUtil", "getIpListByDNSBackup, dns query take time:" + (System.currentTimeMillis() - currentTimeMillis));
        if (dnsResult != null) {
            for (Address address : dnsResult.getAddressList()) {
                if (address.getType().equalsIgnoreCase("A") || address.getType().equalsIgnoreCase("CNAME")) {
                    arrayList.add(address.getValue());
                }
            }
        }
        return arrayList;
    }

    public static List<String> ˊ(String str, long j, Exception exception) {
        List<String> arrayList = new ArrayList();
        try {
            String host = new URL(str).getHost();
            if (ॱ(host)) {
                return arrayList;
            }
            String valueOf;
            DnsResult queryIpsSync;
            Context ˋ = ContentManager.getInstant().getContext();
            RequestHost requestHost = new RequestHost(host);
            requestHost.setApkName(ˋ.getPackageName());
            requestHost.setTime(j);
            if (exception != null) {
                valueOf = String.valueOf(ˏ(0, exception));
                requestHost.setDnsFailType(valueOf);
                Logger.ˋ("DNSUtil", "getNewUrlByDNSBackup, DNSRequest, failReason = " + valueOf);
            }
            long currentTimeMillis = System.currentTimeMillis();
            ˋ();
            try {
                queryIpsSync = DNKeeperManager.getInstance().queryIpsSync(requestHost);
            } catch (Throwable th) {
                Logger.e("DNSUtil", "getNewUrlByDNSBackup, syncQueryDNS exception:" + th.getMessage());
                queryIpsSync = null;
            }
            Logger.ˎ("DNSUtil", "getNewUrlByDNSBackup, dns query take time:" + (System.currentTimeMillis() - currentTimeMillis));
            List<String> arrayList2 = new ArrayList();
            if (queryIpsSync != null) {
                for (Address address : queryIpsSync.getAddressList()) {
                    if (address.getType().equalsIgnoreCase("A") || address.getType().equalsIgnoreCase("CNAME")) {
                        Logger.ˎ("DNSUtil", "getNewUrlByDNSBackup, host=" + host + ", ip=" + address.getValue());
                        arrayList2.add(address.getValue());
                    }
                }
            }
            for (String valueOf2 : arrayList2) {
                arrayList.add(ˎ(str, valueOf2));
            }
            return arrayList;
        } catch (MalformedURLException e) {
            Logger.e("DNSUtil", "getNewUrlByDNSBackup exception:" + e.getMessage());
        }
    }

    private static boolean ॱ(String str) {
        return ˏ.matcher(str).matches();
    }

    public static List<String> ˎ(String str, Exception exception, int i) {
        Future submit = Executors.newSingleThreadExecutor().submit(new d(str, exception, i));
        ArrayList arrayList = new ArrayList();
        try {
            return (List) submit.get(5000, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            Logger.e("GetIpByDNSBackupIplistsCallable", "tryGetIpByDNSBackupLists exception:" + e.toString());
            return arrayList;
        }
    }
}
